package com.atguigu.eduservice.controller;

import com.atguigu.commonutils.OrderVo.OrderCourse;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.vo.CourseInfoForm;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cyt
 * @create 2020-05-07 10:45
 */

@Api(description = "课程管理")

@RequestMapping("/eduservice/course")
@RestController
public class CourseAdminController {

    @Autowired
    EduCourseService eduCourseService;

    @Autowired
    EduTeacherService eduTeacherService;


    //  删除
    @ResponseBody
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id) {
        eduCourseService.delete(id);
        return R.ok();
    }

    // 更新
    @PostMapping("/update")
    public R update(@RequestBody CourseInfoForm courserInfoForm) {
        String courseId = eduCourseService.updateCourse(courserInfoForm);
        if (!StringUtils.isEmpty(courseId)) {
            return R.ok().data("courseId", courseId);
        } else {
            return R.error().message("保存失败");
        }
    }

    // id 查询出课程的信息id
    @GetMapping("/getCourseById/{id}")
    public R getCourseById(@PathVariable("id") String id) {
        CourseInfoForm eduCourse = eduCourseService.getCourseById(id);
        return R.ok().data("eduCourse", eduCourse);
    }

    // id 查询出订单 需要 课程的信息
    @GetMapping("/getOrderCourseById/{id}")
    public OrderCourse getOrderCourse(@PathVariable("id") String id) {
        CourseInfoForm courseInfoForm = eduCourseService.getCourseById(id);

        EduTeacher eduTeacher = eduTeacherService.getById(courseInfoForm.getTeacherId());
        OrderCourse course = new OrderCourse();
        BeanUtils.copyProperties(courseInfoForm, course);
        course.setTeacherName(eduTeacher.getName());
        return course;
    }

    @RequestMapping("/saveCourse")
    public R saveCourse(@RequestBody CourseInfoForm courserInfoForm) {
        String courseId = eduCourseService.saveCourseInfo(courserInfoForm);
        if (!StringUtils.isEmpty(courseId)) {
            return R.ok().data("courseId", courseId);
        } else {
            return R.error().message("保存失败");
        }
    }


    @GetMapping("/getAllChapter/{id}")
    public R getAllChapter(@PathVariable String id) {

        // 根据查出 课程 id 得到它的全部信息
        List<ChapterVo> chapterVolist = eduCourseService.getAllinfo(id);

        return R.ok().data("chapterVolist", chapterVolist);
    }


    @ApiOperation(value = "分页课程列表")
    @GetMapping("{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                    CourseQuery courseQuery) {
        Page<EduCourse> pageParam = new Page<>(page, limit);
        eduCourseService.pageQuery(pageParam, courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }


    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("coursePublishInfo/{id}")
    public R getCoursePublishVoById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id) {
        CoursePublishVo courseInfoForm = eduCourseService.getCoursePublishVoById(id);
        return R.ok().data("item", courseInfoForm);
    }

    @ApiOperation(value = "根据id发布课程")
    @PutMapping("publishCourse/{id}")
    public R publishCourseById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id) {
        eduCourseService.publishCourseById(id);
        return R.ok();
    }

}